package com.demo.controller.flighttime;

import com.demo.common.model.Airroute;
import com.demo.common.model.Flighttime;
import com.demo.service.AircraftService;
import com.demo.service.AirrouteService;
import com.demo.service.FlightTimeService;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

/**
 * 航班验证
 */
public class FlightTimeValidator extends Validator {

    @Inject
    FlightTimeService flightTimeService;

    @Inject
    AircraftService aircraftService;

    @Override
    protected void validate(Controller controller) {
        validateRequiredString("flighttime.flighttimeno", "flighttimenoMsg", "请输入航班编号!");

        validateRequiredString("flighttime.begindate", "begindateMsg", "请选择航班开始时间!");

        validateRequiredString("flighttime.begin", "beginMsg", "请输入航线起点!");

        validateRequiredString("flighttime.end", "endMsg", "请输入航线终点!");

        validateRequiredString("flighttime.enddate", "enddateMsg", "请选择航班结束时间!");

        validateRequiredString("flighttime.airno", "airnoMsg", "请输入飞机编号!");

        validateRequiredString("flighttime.distance", "distanceMsg", "请输入距离路程!");

        String id=controller.getPara("flighttime.id");
        String flighttimeno=controller.getPara("flighttime.flighttimeno");
        String airno=controller.getPara("flighttime.airno");

        if(flightTimeService.findByFlighttimeno(id,flighttimeno)!=null){
            addError("flighttimenoMsg", "航班编号已被添加，请使用别的航班编号！");
        }

        if(aircraftService.findByAirNo(null,airno)==null){
            addError("airnoMsg", "飞机编号不存在，请使用别的飞机编号！");
        }
    }

    @Override
    protected void handleError(Controller controller) {
        controller.keepModel(Flighttime.class);

        String actionKey = getActionKey();
        if (actionKey.equals("/flighttime/save"))
            controller.render("add.html");
        else if (actionKey.equals("/flighttime/update"))
            controller.render("edit.html");
    }
}
